package com.mapbox.mapboxsdk.testapp.view;

import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LockableBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {

  private boolean locked = false;

  public LockableBottomSheetBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setLocked(boolean locked) {
    this.locked = locked;
  }

  @Override
  public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    boolean handled = false;
    if (!locked) {
      handled = super.onInterceptTouchEvent(parent, child, event);
    }
    return handled;
  }

  @Override
  public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    boolean handled = false;
    if (!locked) {
      handled = super.onTouchEvent(parent, child, event);
    }
    return handled;
  }

  @Override
  public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target,
                                     int nestedScrollAxes) {
    boolean handled = false;
    if (!locked) {
      handled = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }
    return handled;
  }

  @Override
  public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy,
                                int[] consumed) {
    if (!locked) {
      super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
    }
  }

  @Override
  public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
    if (!locked) {
      super.onStopNestedScroll(coordinatorLayout, child, target);
    }
  }

  @Override
  public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX,
                                  float velocityY) {
    boolean handled = false;
    if (!locked) {
      handled = super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }
    return handled;
  }
}
